package com.itheima.web.controllor.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.Course;
import com.itheima.service.store.CourseService;
import com.itheima.service.store.impl.CourseServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns="/store/course")
public class CourseServlet extends HttpServlet {
    private CourseService courseService = new CourseServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            //进入学科页面
            this.list(req,resp);
        }else if ("toAdd".equals(operation)){
            this.toAdd(req,resp);
        }else if ("save".equals(operation)){
            this.save(req,resp);
        }else if ("toEdit".equals(operation)){
            this.toEdit(req,resp);
        }else if ("edit".equals(operation)){
            this.edit(req,resp);
        }else if ("delete".equals(operation)){
            this.delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        int rst = courseService.delete(id);
        resp.sendRedirect(req.getContextPath()+"/store/course?operation=list");
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Course course = BeanUtil.fillBean(req, Course.class, "yyyy-MM-dd");
        int rst = courseService.update(course);
        System.out.println(rst);

        resp.sendRedirect(req.getContextPath()+"/store/course?operation=list");
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Course course = courseService.findById(id);
        req.setAttribute("course",course);
        try {
            req.getRequestDispatcher("WEB-INF/pages/store/course/update.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Course course = BeanUtil.fillBean(req, Course.class, "yyyy-MM-dd");
        int rst = courseService.save(course);

        System.out.println(rst);

        resp.sendRedirect(req.getContextPath()+"/store/course?operation=list");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) {
        try {
            req.getRequestDispatcher("WEB-INF/pages/store/course/add.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) {
        int currentPage =1;
        int pageSize = 5;
        if (StringUtils.isNotBlank(req.getParameter("page"))){
            pageSize= Integer.parseInt(req.getParameter("page"));
        }
        if (StringUtils.isNotBlank(req.getParameter("size"))){
            pageSize=Integer.parseInt(req.getParameter("size"));
        }
        PageInfo all = courseService.findAll(currentPage, pageSize);
        req.setAttribute("page",all);

        try {
            req.getRequestDispatcher("/WEB-INF/pages/store/course/list.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
